package com.hiultra.archive.controller;

import com.hiultra.archive.domain.ArcDossier;
import com.hiultra.archive.domain.ArcFamily;
import com.hiultra.archive.service.IArcDossierService;
import com.hiultra.archive.service.IArcFamilyService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.page.TableDataInfo;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 全宗Controller
 *
 * @author ls
 * @date 2022-07-28
 */
@Api(tags="设备管理Controller")
@RestController
@RequestMapping("/base/family")
public class ArcFamilyController extends BaseController {
    @Autowired
    private IArcFamilyService arcFamilyService;
    @Autowired
    private IArcDossierService arcDossierService;

    /**
     * 查询全宗列表
     */
    @ApiOperation(value = "查询全宗列表", notes = "查询全宗列表")
    @PreAuthorize("@ss.hasPermi('base:family:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArcFamily arcFamily) {
        startPage();
        List<ArcFamily> list = arcFamilyService.selectArcFamilyList(arcFamily);
        return getDataTable(list);
    }

    /**
     * 导出全宗列表
     */
    @ApiOperation(value = "导出全宗列表", notes = "导出全宗列表")
    @PreAuthorize("@ss.hasPermi('base:family:export')")
    @Log(title = "全宗", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArcFamily arcFamily) {
        List<ArcFamily> list = arcFamilyService.selectArcFamilyList(arcFamily);
        ExcelUtil<ArcFamily> util = new ExcelUtil<ArcFamily>(ArcFamily.class);
        util.exportExcel(response, list, "全宗数据");
    }

    /**
     * 获取全宗详细信息
     */
    @ApiOperation(value = "获取全宗详细信息", notes = "获取全宗详细信息")
    @PreAuthorize("@ss.hasPermi('base:family:query')")
    @GetMapping(value = "/{familyId}")
    public AjaxResult getInfo(@PathVariable("familyId") Long familyId) {
        return AjaxResult.success(arcFamilyService.selectArcFamilyByFamilyId(familyId));
    }

    /**
     * 新增全宗
     */
    @ApiOperation(value = "新增全宗", notes = "新增全宗")
    @PreAuthorize("@ss.hasPermi('base:family:add')")
    @Log(title = "全宗", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArcFamily arcFamily) {
        if (UserConstants.NOT_UNIQUE.equals(arcFamilyService.checkFamilyCodeUnique(arcFamily))) {
            return AjaxResult.error("新增全宗'" + arcFamily.getFamilyName() + "'失败，全宗号已存在");
        }

        if (UserConstants.NOT_UNIQUE.equals(arcFamilyService.checkFamilyNameUnique(arcFamily))) {
            return AjaxResult.error("新增全宗'" + arcFamily.getFamilyName() + "'失败，全宗名称已存在");
        }
        return toAjax(arcFamilyService.insertArcFamily(arcFamily));
    }

    /**
     * 修改全宗
     */
    @ApiOperation(value = "修改全宗", notes = "修改全宗")
    @PreAuthorize("@ss.hasPermi('base:family:edit')")
    @Log(title = "全宗", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArcFamily arcFamily) {
        if (UserConstants.NOT_UNIQUE.equals(arcFamilyService.checkFamilyCodeUnique(arcFamily))) {
            return AjaxResult.error("修改全宗'" + arcFamily.getFamilyName() + "'失败，全宗号已存在");
        }

        if (UserConstants.NOT_UNIQUE.equals(arcFamilyService.checkFamilyNameUnique(arcFamily))) {
            return AjaxResult.error("修改全宗'" + arcFamily.getFamilyName() + "'失败，全宗名称已存在");
        }
        return toAjax(arcFamilyService.updateArcFamily(arcFamily));
    }

    /**
     * 删除全宗
     */
    @ApiOperation(value = "删除全宗", notes = "删除全宗")
    @PreAuthorize("@ss.hasPermi('base:family:remove')")
    @Log(title = "全宗", businessType = BusinessType.DELETE)
    @DeleteMapping("/{familyIds}")
    public AjaxResult remove(@PathVariable Long[] familyIds) {

        //查询是否被引用
        for (Long familyId : familyIds) {
             List<ArcDossier> dossierList = arcDossierService.selectArcDossierList(new ArcDossier() {{
                setFamilyId(familyId);
            }});
            if (dossierList.size() > 0) {
                return AjaxResult.error("全宗序号" + familyId + "被引用，无法删除！");
            }
        }
        return toAjax(arcFamilyService.deleteArcFamilyByFamilyIds(familyIds));
    }
}
